/****** Object:  Stored Procedure dbo.CGO_AccountLogGetList    Script Date: Sunday, March 06, 2011 ******/
IF EXISTS ( SELECT  *
            FROM    dbo.sysobjects
            WHERE   id = OBJECT_ID(N'dbo.CGO_AccountLogGetList')
                    AND OBJECTPROPERTY(id, N'IsProcedure') = 1 ) 
  DROP PROCEDURE dbo.CGO_AccountLogGetList
GO


/* -------------------------------------------------------------------------------------
/   GameEnrollmentGetList 
/  ------------------------------------------------------------------------------------- */
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE dbo.CGO_AccountLogGetList
  @PortalID INT ,
  @UserID INT ,
  @GetCount BIT = 0,
  @PageSize INT = 10,
  @CurrentPage INT = 1
AS 
  DECLARE @TotalRowCount INT
  SET @TotalRowCount = 0
  IF @UserID IS NULL 
    BEGIN
      SET @UserID = 0
    END
  IF @GetCount IS NULL
    BEGIN
    	SET @GetCount = 0
    END
  IF @PageSize IS NULL
    BEGIN
  	  SET @PageSize = 10
    END
  IF @CurrentPage IS NULL
    BEGIN
  	  SET @CurrentPage = 1
    END
  
  IF @GetCount = 1
  BEGIN
    SELECT @TotalRowCount = COUNT(*)
    FROM   dbo.CGO_AccountLog M
    WHERE  M.PortalID = @PortalID
          AND ( @UserID = 0
                OR M.UserID = @UserID
              )          
  END
  
  SET ROWCOUNT @PageSize
  SELECT  @TotalRowCount AS TotalRowCount, T.*
  FROM    ( SELECT  ROW_NUMBER() OVER ( ORDER BY LogTime DESC) AS RANK ,
                    AccountLogID,
                    PortalID,
                    UserID,
                    Token,                    
                    PaymentID,
                    Comment,
                    LogTime
            FROM    dbo.CGO_AccountLog
            WHERE   PortalID = @PortalID
                    AND ( @UserID = 0
                          OR UserID = @UserID
                        )                    
          ) T
  WHERE   T.[RANK] > ( @CurrentPage - 1 ) * @PageSize
  
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO